﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;

namespace Qianli.DirSir.Common
{
    /// <summary>
    /// MS-SQL数据库访问类
    /// </summary>
    public class SQLHelper
    {
        private SqlConnection SQLConn = null;
        private bool IsReadOnly = false;
        /// <summary>
        /// 构造函数
        /// </summary>
        public SQLHelper(bool isReadOnly)
        {
            this.IsReadOnly = isReadOnly;
            if (SQLConn == null)
            {
                SQLConn = new SqlConnection(MyConnStr);
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public SQLHelper()
        {
            if (SQLConn == null)
            {
                SQLConn = new SqlConnection(MyConnStr);
            }
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        private string MyConnStr
        {
            get
            {
                string ConfigStr = string.Empty;



                //ConfigStr = "Data Source=想我所爱;Initial Catalog=20161010;User ID=sa;Pwd=dhp970504";
                //ConfigStr = "Data Source=169.254.57.33;Initial Catalog=20161010;User ID=sa;Pwd=dhp970504";
                ConfigStr = "Data Source=.;Initial Catalog=kk1;Integrated Security=True";

                return ConfigStr;
            }
        }
        /// <summary>
        /// 参数缓存哈希表
        /// </summary>
        private Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        #region 执行SQL语句
        /// <summary>
        /// 执行SQL语句,返回数据库中影响的行数
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public int ExecNonquery(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);

                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                this.Close();
                return val;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回数据库中影响的行数
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public int ExecNonquery(string cmdText)
        {
            return ExecNonquery(cmdText, null);
        }

        /// <summary>
        /// 执行SQL语句,返回数据中第一行第一列的值
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public object ExecScalar(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                this.Close();
                return val;
            }
            catch
            {
                this.Close();
                return -1;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回数据中第一行第一列的值
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public object ExecScalar(string cmdText)
        {
            return ExecScalar(cmdText, null);
        }

        /// <summary>
        /// 执行SQL语句,返回一个ExecuteReader
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public SqlDataReader ExecReader(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回一个ExecuteReader
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public SqlDataReader ExecuteReader(string cmdText)
        {
            return ExecReader(cmdText, null);
        }

        /// <summary>
        /// 执行SQL语句,返回DataSet
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public DataSet ExecDataSet(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cmd.Parameters.Clear();
                this.Close();
                return ds;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回DataSet
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public DataSet ExecDataSet(string cmdText)
        {
            return ExecDataSet(cmdText, null);
        }

        /// <summary>
        /// 执行SQL语句,返回DataTable
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public DataTable ExecDataTable(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                cmd.Parameters.Clear();
                this.Close();
                return dt;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回DataSet
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public DataTable ExecDataTable(string cmdText)
        {
            return ExecDataTable(cmdText, null);
        }

        /// <summary>
        /// 执行SQL语句,返回ExecuteXmlReader
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public XmlReader ExecXmlReader(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.Text, commandParameters);
                XmlReader xr = cmd.ExecuteXmlReader();
                xr.MoveToElement();
                xr.Close();

                return xr;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句,返回ExecuteXmlReader
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public XmlReader ExecXmlReader(string cmdText)
        {
            return ExecXmlReader(cmdText, null);
        }
        #endregion

        #region 执行存储过程

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="tableName">DataSet结果中的表名</param>
        /// <returns>DataSet</returns>
        public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            using (SqlConnection connection = new SqlConnection(MyConnStr))
            {
                DataSet dataSet = new DataSet();
                connection.Open();
                SqlDataAdapter sqlDA = new SqlDataAdapter();
                sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                sqlDA.Fill(dataSet, tableName);
                connection.Close();
                return dataSet;
            }
        }
        /// <summary>
        /// 构建 SqlCommand 对象(用来返回一个结果集，而不是一个整数值)
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand</returns>
        private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in parameters)
            {
                if (parameter != null)
                {
                    // 检查未分配值的输出参数,将其分配以DBNull.Value.
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
            }

            return command;
        }
        /// <summary>
        /// 执行存储过程,返回数据库中影响的行数
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public int ExecNonquery_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                this.Close();
                return val;
            }
            catch
            {

                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行存储过程,返回数据库中影响的行数
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public int ExecNonquery_Proc(string cmdText)
        {
            return ExecNonquery_Proc(cmdText, null);
        }

        /// <summary>
        /// 执行存储过程,返回数据中第一行第一列的值
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public object ExecScalar_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                this.Close();
                return val;
            }
            catch
            {
                this.Close();
                throw;
            }
        }
        /// <summary>
        /// 执行存储过程，返回一个数组
        /// 返回的数组是:array[0]是一个DataSet,即执行该过程的n个表
        ///				 array[1]是一个ArrayList,即输出参数的值
        /// </summary>
        /// <param name="cmd">SqlCommand</param>
        /// <returns></returns>
        public object[] ExecProcedures(SqlCommand cmd)
        {
            object[] objs = new object[2];
            ArrayList outvalue = new ArrayList();
            DataSet ds = new DataSet();
            cmd.Connection = this.SQLConn;
            SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(cmd);
            objSqlDataAdapter.Fill(ds);
            for (int i = 0; i < cmd.Parameters.Count; i++)
            {
                if (cmd.Parameters[i].Direction == System.Data.ParameterDirection.Output || cmd.Parameters[i].Direction == System.Data.ParameterDirection.InputOutput)
                {
                    outvalue.Add(cmd.Parameters[i].Value);
                }

            }
            objs[0] = ds;
            objs[1] = outvalue;
            return objs;
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="OutParam">输出参数</param>
        /// <param name="commandParameters">参数列表</param>
        /// <returns></returns>
        public object ExecProcedures(string cmdText, string OutParam, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                int n = cmd.ExecuteNonQuery();
                object o = cmd.Parameters[OutParam].Value;
                cmd.Parameters.Clear();
                return o;

            }
            catch
            {
                this.Close();
                throw;
            }
        }
        /// <summary>
        /// 执行存储过程,返回数据中第一行第一列的值
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public object ExecScalar_Proc(string cmdText)
        {
            return ExecScalar_Proc(cmdText, null);
        }

        /// <summary>
        /// 执行存储过程,返回一个ExecuteReader
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public SqlDataReader ExecReader_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行存储过程,返回一个ExecuteReader
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public SqlDataReader ExecReader_Proc(string cmdText)
        {
            return ExecReader_Proc(cmdText, null);
        }

        /// <summary>
        /// 执行存储过程,返回DataSet
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public DataSet ExecDataSet_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cmd.Parameters.Clear();
                this.Close();
                return ds;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行存储过程,返回DataSet
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public DataSet ExecDataSet_Proc(string cmdText)
        {
            return ExecDataSet_Proc(cmdText, null);
        }

        /// <summary>
        /// 执行存储过程,返回DataTable
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public DataTable ExecDataTable_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                cmd.Parameters.Clear();
                this.Close();
                return dt;
            }
            catch
            {
                this.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行存储过程,返回DataSet
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public DataTable ExecDataTable_Proc(string cmdText)
        {
            return ExecDataTable_Proc(cmdText, null);
        }

        /// <summary>
        /// 执行存储过程,返回ExecuteXmlReader
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public XmlReader ExecXmlReader_Proc(string cmdText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                CommonPreCmd(cmdText, cmd, null, CommandType.StoredProcedure, commandParameters);
                XmlReader xr = cmd.ExecuteXmlReader();
                xr.MoveToElement();
                xr.Close();

                return xr;
            }
            catch
            {
                this.Close();
                return null;
            }
        }

        /// <summary>
        /// 执行存储过程,返回ExecuteXmlReader
        /// </summary>
        /// <param name="cmdText">存储过程</param>
        /// <returns></returns>
        public XmlReader ExecXmlReader_Proc(string cmdText)
        {
            return ExecXmlReader_Proc(cmdText, null);
        }
        #endregion

        #region 执行SQL命令
        public int ExecNonquery(SqlCommand cmd)
        {
            try
            {
                cmd.Connection = this.SQLConn;
                this.Open();
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                this.Close();
                return val;
            }
            catch
            {
                this.Close();
                throw;
            }
        }
        public object ExecScalar(SqlCommand cmd)
        {
            try
            {
                cmd.Connection = this.SQLConn;
                this.Open();
                object obj = cmd.ExecuteScalar();
                this.Close();
                return obj;
            }
            catch
            {
                this.Close();
                throw;
            }
        }
        public DataTable ExecDataTable(SqlCommand cmd)
        {
            try
            {
                cmd.Connection = this.SQLConn;
                this.Open();
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                this.Close();
                return dt;
            }
            catch
            {
                throw;
            }
        }
        public DataSet ExecDataSet(SqlCommand cmd)
        {
            try
            {
                cmd.Connection = this.SQLConn;
                this.Open();
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                this.Close();
                return ds;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 外部公共方法
        /// <summary>
        /// 打开连接
        /// </summary>
        public void Open()
        {
            if (this.SQLConn == null)
            {
                this.SQLConn = new SqlConnection(this.MyConnStr);
            }
            if (this.SQLConn != null && this.SQLConn.State == ConnectionState.Closed)
            {
                this.SQLConn.Open();
            }
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void Close()
        {
            if (this.SQLConn != null && this.SQLConn.State == ConnectionState.Open)
            {
                this.SQLConn.Close();
                this.SQLConn.Dispose();
            }
        }
        #endregion

        #region 内部公共方法
        /// <summary>
        /// 把需要传递的参数放入缓存中
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="commandParameters"></param>
        void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            parmCache[cacheKey] = commandParameters;
        }

        /// <summary>
        /// 操作时把放入缓存中的参数取出来
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];

            if (cachedParms == null)
                return null;

            SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];

            for (int i = 0, j = cachedParms.Length; i < j; i++)
                clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();

            return clonedParms;
        }

        /// <summary>
        /// 对命令属性进行初始化
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdParms"></param>
        void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

        /// <summary>
        /// 对命令属性进行初始化
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdParms"></param>
        void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms, string OutParam)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
                cmd.Parameters[OutParam].Direction = ParameterDirection.Output;
            }
        }
        /// <summary>
        /// 对命令属性进行初始化（重载，无parms参数）
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText)
        {
            if (conn == null)
            {
                conn = new SqlConnection(MyConnStr);
            }
            if (conn.State != ConnectionState.Open)
                conn.Open();


            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;
        }
        /// <summary>
        /// 初始化各项数据
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmd"></param>
        /// <param name="trans"></param>
        /// <param name="cmdType"></param>
        /// <param name="commandParameters"></param>
        void CommonPreCmd(string cmdText, SqlCommand cmd, SqlTransaction trans, CommandType cmdType, params SqlParameter[] commandParameters)
        {
            SqlConnection SQLConn = new SqlConnection();
            SQLConn.ConnectionString = MyConnStr;
            if (commandParameters != null)
            {

                PrepareCommand(cmd, SQLConn, trans, cmdType, cmdText, commandParameters);
            }
            else
            {

                PrepareCommand(cmd, SQLConn, trans, cmdType, cmdText);
            }
        }
        #endregion

        #region 事务处理
        /// <summary>
        /// 事务提交多个数据库操作,返回每一次执行影响的行数列表
        /// </summary>
        /// <param name="cmdTextList">SQL语句组</param>
        /// <returns></returns>
        public List<int> ExecTransaction(List<string> cmdTextList)
        {
            List<int> ExecQueryList = new List<int>();
            string timePoint = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string transName = "TRANS" + timePoint;
            this.Open();
            SqlCommand cmd = this.SQLConn.CreateCommand();
            SqlTransaction trans = this.SQLConn.BeginTransaction(transName);
            cmd.Connection = this.SQLConn;
            cmd.Transaction = trans;
            try
            {
                trans.Save(transName);
                for (int i = 0; i < cmdTextList.Count; i++)
                {
                    cmd.CommandText = cmdTextList[i];
                    if (cmd.ExecuteNonQuery() >= 1)
                    {
                        ExecQueryList.Add(1);
                    }
                    else
                    {
                        trans.Rollback(transName);
                        this.Close();
                        return ExecQueryList;
                    }
                }
                trans.Commit();
            }
            catch
            {
                trans.Rollback(transName);
            }
            this.Close();
            return ExecQueryList;
        }

        /// <summary>
        /// 事务提交多个数据库操作,返回每一次执行影响的行数列表
        /// </summary>
        /// <param name="cmdTextList">SQL语句组</param>
        /// <param name="cmdParametersList">SQL参数列表集合</param>
        /// <returns></returns>
        public List<int> ExecTransaction(List<string> cmdTextList, List<List<SqlParameter>> cmdParametersList)
        {
            if (cmdTextList.Count != cmdParametersList.Count)
            {
                throw new Exception("SQL语句数与参数列表数不相等");
            }

            List<int> ExecQueryList = new List<int>();
            string timePoint = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string transName = "TRANS" + timePoint;
            this.Open();
            SqlCommand cmd = this.SQLConn.CreateCommand();
            SqlTransaction trans = this.SQLConn.BeginTransaction(transName);
            cmd.Connection = this.SQLConn;
            cmd.Transaction = trans;
            try
            {
                trans.Save(transName);
                for (int i = 0; i < cmdTextList.Count; i++)
                {
                    cmd.CommandText = cmdTextList[i];
                    cmd.Parameters.AddRange(cmdParametersList[i].ToArray());
                    if (cmd.ExecuteNonQuery() >= 1)
                    {
                        ExecQueryList.Add(1);
                    }
                    else
                    {
                        trans.Rollback(transName);
                        this.Close();
                        return ExecQueryList;
                    }
                    cmd.Parameters.Clear();
                }
                trans.Commit();
            }
            catch
            {
                trans.Rollback(transName);
            }
            this.Close();
            return ExecQueryList;
        }
        #endregion

        #region 批量操作
        /// <summary>
        /// 批量插入数据库 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public bool SqlBulkCopy(DataTable dt)
        {
            SqlBulkCopy sqlbulk = new SqlBulkCopy(this.MyConnStr, SqlBulkCopyOptions.UseInternalTransaction);
            try
            {
                sqlbulk.NotifyAfter = dt.Rows.Count;
                sqlbulk.BulkCopyTimeout = 1200;
                //目标数据库表名
                sqlbulk.DestinationTableName = dt.TableName;
                //数据集字段索引与数据库字段索引映射
                foreach (DataColumn item in dt.Columns)
                {
                    sqlbulk.ColumnMappings.Add(item.ColumnName, item.ColumnName);
                }

                //导入
                sqlbulk.WriteToServer(dt);
                sqlbulk.Close();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                sqlbulk.Close();
                dt.Dispose();
                this.Close();
            }
        }
        /// <summary>
        /// 批量更新事件
        /// </summary>
        /// <param name="dt"></param>

        /// <returns></returns>
        public int SqlUpdateBatch(DataTable dt)
        {
            SqlConnection sqlcon = new SqlConnection(this.MyConnStr);
            sqlcon.Open();
            SqlCommand sqlcomand = new SqlCommand("select top 0 * from " + dt.TableName + "(nolock)", sqlcon);
            DataTable dts = new DataTable();
            SqlDataAdapter Sda = new SqlDataAdapter(sqlcomand);
            try
            {
                Sda.Fill(dts);
                foreach (DataRow item in dt.Rows)
                {
                    dts.Rows.Add(item.ItemArray);
                }
                Sda.UpdateBatchSize = 1000;
                SqlCommandBuilder Scb = new SqlCommandBuilder(Sda);
                return Sda.Update(dts);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                sqlcon.Close();
                sqlcon.Dispose();
                Sda.Dispose();
                dt.Dispose();
                this.Close();
            }
        }
        #endregion
    }
}